//
//  MSDetailDescView.m
//  MSJD
//
//  Created by xiangbo on 2018/11/7.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSDescDetailView.h"

@implementation MSDescDetailView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubviews];
        [self addSubviewConstraints];
    }
    return self;
}

- (void)setupSubviews{
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:15.f];
    self.titleLabel.textColor = UIColorHex(0x444444);
    self.titleLabel.text = @"这是标题";
    
    self.star = [[MSStarControl alloc] initWithFrame:CGRectMake(0.f, 0.f, 80.f, 12)];
    self.star.allowIncompleteStar = YES;
    
    self.scoreLabel = [[UILabel alloc] init];
    self.scoreLabel.font = [UIFont systemFontOfSize:12.f];
    self.scoreLabel.textColor = UIColorHex(0x444444);
    self.scoreLabel.text = @"4.8";
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont systemFontOfSize:12.f];
    self.priceLabel.textColor = UIColorHex(0xf96332);
    self.priceLabel.text = @"88元/位";
    
//    self.location = [MSLocationButton buttonWithType:UIButtonTypeCustom];
    self.locImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shape"]];
    [self addSubview:self.locImage];

    self.descLabel = [[UILabel alloc] init];
    self.descLabel.font = [UIFont systemFontOfSize:13.f];
    self.descLabel.textColor = MSSubTitleColor;
    self.descLabel.text = @"这是其他描述";
    
    self.distanceLabel = [[UILabel alloc] init];
    self.distanceLabel.font = [UIFont systemFontOfSize:12.f];
    self.distanceLabel.textColor = UIColorHex(0x444444);
    self.distanceLabel.text = @"3.2km";
    self.distanceLabel.textAlignment = NSTextAlignmentRight;
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.star];
    [self addSubview:self.scoreLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.locImage];
    [self addSubview:self.descLabel];
//    [self addSubview:self.location];
    [self addSubview:self.distanceLabel];
}

-(void)addSubviewConstraints{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self);
        make.right.equalTo(self);
    }];
    
    [self.star mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
        make.width.mas_equalTo(self.star.width);
        make.height.mas_equalTo(self.star.height);
    }];
    
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.star.mas_right).offset(6);
        make.centerY.equalTo(self.star);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scoreLabel.mas_right).offset(8);
        make.centerY.equalTo(self.star);
    }];
    
//    [self.location mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.titleLabel);
//        make.top.equalTo(self.star.mas_bottom).offset(10);
//    }];
    
    [self.locImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.priceLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(self.locImage.width);
        make.height.mas_equalTo(self.locImage.height);
    }];

    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.locImage.mas_right).offset(6);
        make.centerY.equalTo(self.locImage);
//        make.width.priority(MASLayoutPriorityDefaultHigh);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-3);
        make.centerY.equalTo(self.descLabel);
        make.left.greaterThanOrEqualTo(self.descLabel.mas_right).offset(4);
//        make.width.priority(MASLayoutPriorityDefaultMedium);
    }];
}


- (void)setContentData:(id)contentData{
    NSString *title = @"";
    NSNumber *score = @(0);
    NSString *price = @"";
    NSString *location = @"";
    NSString *distance = @"";
    if ([contentData isKindOfClass:[MSFoodContentModel class]]) {
        MSFoodContentModel *model = (MSFoodContentModel *)contentData;
        title = model.title;
        score = model.score;
        price = model.subTitle;
        location = model.location;
    }else if ([contentData isKindOfClass:[MSFoodDetailData class]]) {
        MSFoodDetailData *model = (MSFoodDetailData *)contentData;
        title = model.itemName;
        score = model.score;
        price = [NSString stringWithFormat:@"%@元/位",model.price];
        location = model.location;
    }
    
    self.titleLabel.text = title;
    self.scoreLabel.text = score.stringValue;
    self.star.scorePercent = score.floatValue;
    self.priceLabel.text = price;
    self.priceLabel.hidden = price.length == 0;
//    [self.location setTitle:location forState:UIControlStateNormal];
    self.descLabel.text = location;
    self.distanceLabel.text = distance;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
